package know.how.glutenfree;

import java.util.List;


import com.bappi.custom.activities.CustomActivity;
import com.bappi.utils.HttpUtils;
import com.bappi.utils.Utils;
import com.goethe.utils.db.DatabaseAccessor;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class RestaurantDetailsActivity extends CustomActivity {

	public static final String KEY_ID = "KEY_ID";
	private TextView titleView;
	private ListView listView;
	private Button addOrRemoveFavorites;
	private RestaurantItem listItem;
	private List<RestaurantDetailsItem> rDetailsItems;
	private EfficientAdapter adapter;
	

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.layout_restaurant_details);
		titleView = (TextView)findViewById(R.id.title_view);
		addOrRemoveFavorites = (Button)findViewById(R.id.button_add_to_favorites);
		listItem = TabsController.getRestaurent(getIntent().getIntExtra(KEY_ID, 0));
		titleView.setText(listItem.getTitle());
		listView = (ListView) findViewById(R.id.list_view);

		if(listItem.isFavorite()){
			addOrRemoveFavorites.setText("Remove from Favorites");
		}else{
			addOrRemoveFavorites.setText("Add to Favorites");
		}
		addOrRemoveFavorites.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				
				if(listItem.isFavorite()){
					TabsController.removeFromFavorite(listItem);
					addOrRemoveFavorites.setText("Add to Favorites");
				}else{
					TabsController.addToFavorite(listItem);
					addOrRemoveFavorites.setText("Remove from Favorites");
					
				}
				
			}
		});
		
		new AsyncTask<String, String, String>() {
			protected String doInBackground(String... arg0) {
				try {
					rDetailsItems = DatabaseAccessor.getRestaurantDetails(listItem.getId());
					adapter = new EfficientAdapter(getParent());
				} catch (Exception e) {
					e.printStackTrace();
				}
				return null;
			}
			@Override
			protected void onPostExecute(String unused) {
				try {
					listView.setAdapter(adapter);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.execute();
	}
	
	public class EfficientAdapter extends BaseAdapter {
		private LayoutInflater mInflater;

		public EfficientAdapter(Context context) {
			mInflater = LayoutInflater.from(context);
		}

		public View getView(final int position, View convertView,
				ViewGroup parent) {
			try {
				ViewHolder holder;
				if (convertView == null) {
					convertView = mInflater
							.inflate(R.layout.menu_list_content, null);
					holder = new ViewHolder();
					holder.mainContainer = convertView
							.findViewById(R.id.main_container);
					holder.titleView = (TextView) convertView
							.findViewById(R.id.title);
					holder.subtitleView = (TextView) convertView
							.findViewById(R.id.sub_title);
					holder.detailsView = (TextView) convertView
							.findViewById(R.id.content);
					holder.footerView = (TextView) convertView
							.findViewById(R.id.footer);
					holder.imageView = (ImageView) convertView
							.findViewById(R.id.image_view);
					convertView.setClickable(true);
					convertView.setTag(holder);
				} else {
					holder = (ViewHolder) convertView.getTag();
				}
				
				final RestaurantDetailsItem rdi = rDetailsItems.get(position);
				
				if(rdi.getTitle()!=null && rdi.getTitle().length()>0){
					holder.titleView.setVisibility(View.VISIBLE);
					holder.titleView.setText(rdi.getTitle());
				}else{
					holder.titleView.setVisibility(View.GONE);
				}
				
				if(rdi.getSubTitle()!=null && rdi.getSubTitle().length()>0){
					holder.subtitleView.setVisibility(View.VISIBLE);
					holder.subtitleView.setText(rdi.getSubTitle());
				}else{
					holder.subtitleView.setVisibility(View.GONE);
				}
				
				if(rdi.getDetails()!=null && rdi.getDetails().length()>0){
					holder.detailsView.setVisibility(View.VISIBLE);
					holder.detailsView.setText(rdi.getDetails());
				}else{
					holder.detailsView.setVisibility(View.GONE);
				}
				
				if(rdi.getFooter()!=null && rdi.getFooter().length()>0){
					holder.footerView.setVisibility(View.VISIBLE);
					holder.footerView.setText(rdi.getFooter());
				}else{
					holder.footerView.setVisibility(View.GONE);
				}
				
				if(rdi.getImage()!=null && rdi.getImage().length()>0){
					Log.i("DREG", "rdi.getImage()="+rdi.getImage().substring(0,rdi.getImage().lastIndexOf('.')));
					
					final ImageView imageView = holder.imageView;
					imageView.setVisibility(View.VISIBLE);
//					Drawable drawable = getResources().getDrawable(
//							getResources().getIdentifier(
//									rdi.getImage(), "drawable",
//									getClass().getPackage().getName()));
					
					imageView.setImageResource(getResources().getIdentifier(
							rdi.getImage().substring(0,rdi.getImage().lastIndexOf('.')), "drawable",
							getClass().getPackage().getName()));
//					imageView.post(new Runnable() {
//						
//						@Override
//						public void run() {
//							int width = getWindowManager().getDefaultDisplay().getWidth();
//							Bitmap bitmap = BitmapFactory.decodeResource(getResources(), getResources().getIdentifier(
//							rdi.getImage().substring(0,rdi.getImage().lastIndexOf('.')), "drawable",
//							getClass().getPackage().getName()));
//							imageView.setImageBitmap(ImageUtils.getResizeImage(bitmap, width, bitmap.getHeight()*width/bitmap.getWidth()));
//							
//						}
//					});
				}else{
					Log.i("DREG", "rdi.getImage()=null");
					holder.imageView.setVisibility(View.GONE);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return convertView;
		}

		class ViewHolder {
			View mainContainer;
			TextView titleView;
			TextView subtitleView;
			TextView detailsView;
			TextView footerView;
			ImageView imageView;
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		public int getCount() {
			return rDetailsItems.size();
		}

		public Object getItem(int position) {
			return rDetailsItems.get(position);
		}
	}
	
	
}